package org.example;

import org.apache.ibatis.session.SqlSession;
import org.example.dao.StudentDao;
import org.example.entity.Student;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class App {
    public static void main(String args[]){
        /**
         * 测试使用Mybatis创建StudentDao来取数据
         *1.创建一个与Mybatis的对话连接Session--启动mybatis
         */

        SqlSession session = SqlSessionFactoryUtils.openSession();
        //2.利用Mybatis创建一个StudentDao对象，不能new
        StudentDao dao = session.getMapper(StudentDao.class);
        //3.1测试执行getAll方法
//        List<Student> list = dao.getAll();
        //3.2测试，执行getList方法
        Map params = new HashMap<>();
        //params.put("name", "魏");
        params.put("no","122");
        List<Student> list = dao.getList(params);
        //4.输出结果
        System.out.println("得到的数据个数：" + list.size());
        for (Student obj : list){
            System.out.println("学号：" + obj.getNo() + ",姓名：" + obj.getName());
        }
        //新增测试
        Student student = new Student();
        student.setNo("200");
        student.setName("wang");
        student.setClassId(1);

        int n = dao.add(student);
        session.commit();//提交
        if(n > 0)
            System.out.println("增加成功！");
    }
}
